package com.sam.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 * @author Sam.Lee
 * @version Created：Apr 11, 2012 9:27:50 AM
 */
@SuppressWarnings("serial")
public class DDLConvertFieldTypeByCondition extends JFrame {

	private File getSourceFile() {
		File file = null;
		JFileChooser jFileChooser = new JFileChooser();
		int tmp = jFileChooser.showSaveDialog(this);
		if (tmp == JFileChooser.APPROVE_OPTION) {
			file = jFileChooser.getSelectedFile();
		}
		return file;
	}

	public static void main(String[] args) {
		File f1 = new DDLConvertFieldTypeByCondition().getSourceFile();
		File f2 = new File(f1.getParent() + "\\[1]" + f1.getName());
		String strLine = null;
		try {
			FileReader reader = new FileReader(f1);
			FileWriter writer = new FileWriter(f2);
			BufferedReader br = new BufferedReader(reader);
			BufferedWriter bw = new BufferedWriter(writer);
			while ((strLine = br.readLine()) != null) {
				int startPosition = strLine.indexOf("VARCHAR(");
				if (startPosition > -1) {
					int endPosition = strLine.indexOf(")");
					int varcharLen = Integer.parseInt(strLine.substring(
							startPosition + 8, endPosition));
					if (varcharLen <= 255) {
						strLine = strLine.replace(
								"VARCHAR(" + varcharLen + ")", "CHAR("
										+ varcharLen + ")");
					}
				}
				System.out.println(strLine);
				bw.write(strLine);
				bw.newLine();
				bw.flush();
			}
			br.close();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
